#include "lightboard.h"
#include "drv_pin.h"
#include "drv_timer.h"
#include "main.h"
#include "tm1650.h"
#include "systemdata.h"
#include "wakeup.h"
#include <stdio.h>
#include "delay.h"

//10段LED电量板功能
//充电:灯板一直电量自增显示
//其他状态:按下灯板按键后亮10秒
//每格代表10%电量，余数1~5，则交替闪烁，若大于6~9，则常量。
//例：15%电量，第一格常量，第二格交替闪烁；17电量时，两格常亮。

typedef enum{
	ltbd_catch_signal = 0x00,
	ltbd_pre_show = 0x03,
	ltbd_kp_show = 0x04,
}LTBD_CLTR_STEP_t;

typedef struct{
	LTBD_CLTR_STEP_t 	step;//控制步骤
	uint32_t			timer_1ms_flag;
	uint32_t			timer_ms_cnt;
	uint32_t			total_powerd_time;//打开后显示计时
	uint8_t				show_gird;//显示的格数
}LTBD_CTRL_DATA_t;

static SysRunData_t *runinfo = NULL;
static Timer_t *ltbd_timer = NULL;
static LTBD_CTRL_DATA_t  ctrl_data = {0};

static void key_signal_catch(void){//按键捕获状态
	if(runinfo->wakeState.bits.LIGHT_WAKE || runinfo->batState == chgState){
		runinfo->wakeState.bits.LIGHT_WAKE = 0;
		hc32_pin_write(MCU_KZ_LIGHTBOARD_PIN,PIN_HIGH);
		Delay_ms(10);
		light_board_sleep_exit();
		tm1650_set_value(0);//显示清零
		ctrl_data.timer_ms_cnt = 0;
		ctrl_data.step = ltbd_pre_show;
	}
}

static void ltbd_prepare_show(void){
	uint8_t showGirdNum = runinfo->batSoc/10;
	if(runinfo->batSoc%10 > 5){
		showGirdNum ++;
	}
	if(showGirdNum == 0){
		ctrl_data.step = ltbd_kp_show;//如果要显示的格数为0，直接切换到显示保持阶段
		return;
	}
	if(ctrl_data.timer_ms_cnt >= 200){
		ctrl_data.timer_ms_cnt = 0;
		ctrl_data.show_gird ++;
		tm1650_set_value(ctrl_data.show_gird);
	}
	if(ctrl_data.show_gird >= showGirdNum){
		ctrl_data.step = ltbd_kp_show;
	}
}

static void ltbd_keep_show(void){
	uint8_t showGirdNum = runinfo->batSoc/10;
	uint8_t temp = 0;//交替闪烁标志
	if(runinfo->batSoc%10 > 5){
		showGirdNum ++;
	}else if(runinfo->batSoc%10 >= 1){//SOC的余数在1-4之间
		temp = 1;
	}
	if(ctrl_data.timer_ms_cnt >= 500){//闪烁周期为1秒
		if(temp ==1){
			temp = 0;
			if(ctrl_data.show_gird == showGirdNum){//需要闪烁的那一格没有点亮
				ctrl_data.show_gird ++;
			}else{
				ctrl_data.show_gird --;
			}
		}
		tm1650_set_value(ctrl_data.show_gird);
		ctrl_data.total_powerd_time += ctrl_data.timer_ms_cnt;
		ctrl_data.timer_ms_cnt = 0;
	}
	if(ctrl_data.total_powerd_time >= 10*1000){//从保持显示开始，累计显示10秒
		if(runinfo->batState == chgState){
			ctrl_data.total_powerd_time = 10*1000;//将计时值保持在阈值，便于充电结束后立马熄灭
		}else{//不在充电状态且计时达到
			ctrl_data.total_powerd_time = 0;
			light_board_sleep_enter();
//			hc32_pin_write(MCU_KZ_LIGHTBOARD_PIN,PIN_LOW);
//			memset(&ctrl_data,0,sizeof(ctrl_data));                  
//			wake_enable(light_wake_bit,Enable);
		}
	}
}

void light_board_process(void){
	
	if(ctrl_data.timer_1ms_flag == 0){
		return;
	}
	if(ctrl_data.step == ltbd_catch_signal){	//捕获唤醒信号，包含按键信号和充电信号
		key_signal_catch();								
	}else if(ctrl_data.step == ltbd_pre_show){	//预显示阶段
		ltbd_prepare_show();
	}if(ctrl_data.step == ltbd_kp_show){		//显示保持阶段
		ltbd_keep_show();
	}
	ctrl_data.timer_1ms_flag = 0;
}

void light_board_sleep_enter(void){
	tm1650_sleep_enter();
	hc32_pin_write(MCU_KZ_LIGHTBOARD_PIN,PIN_LOW);
	hc32_pin_write(LED_BOARD_SCL_PIN,PIN_LOW);
	hc32_pin_write(LED_BOARD_SDA_PIN,PIN_LOW);
	hc32_pin_write(DSI_NC_PIN,PIN_LOW);
	memset(&ctrl_data,0,sizeof(ctrl_data));
//	wake_enable(light_wake_bit,Enable);
}

void light_board_sleep_exit(void){
	tm1650_sleep_exit();
}

static void ltbd_timer_callback(void *data){
	ctrl_data.timer_1ms_flag = 1;
	ctrl_data.timer_ms_cnt += *(uint32_t *)data;
}


void light_board_init(void){
	runinfo = getSysRunData();
	tm1650_init();
	ltbd_timer = timer_create(ltbd_timer_callback,1);
	timer_start(ltbd_timer);
}
